package com.tanhua.server.interceptor;

import com.tanhua.model.domain.User;
import org.apache.commons.collections.set.UnmodifiableSet;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/29 20:47
 *
 *  ThreadLocal 同一线程共享数据
 *
 */

public class UserHolderThreadLocal {

    private static ThreadLocal<User> threadLocal = new ThreadLocal();

    /**
     * 保存数据到ThreadLocal
     * @param user
     */
    public static void set(User user){
        threadLocal.set(user);
    }

    /**
     * 获取数据
     * @return
     */
    public static User get(){
        User user = threadLocal.get();
        return user;
    }

    /**
     * 删除ThreadLocal中的数据
     */
    public static void delete(){
        threadLocal.remove();
    }

    /**
     * 获取用户id、
     *
     * @return
     */
    public static Long getId(){

        User user = threadLocal.get();
        if(user.getId() == null){
            return null;
        }
        return user.getId();
    }

    /**
     * 获取用户手机号码
     * @return
     */
    public static String getMobial(){

        User user = threadLocal.get();
        if(user.getMobile() == null){
            return null;
        }
        return user.getMobile();
    }

}
